/*
 * Copyright Notice ====================================================
 * This file contains proprietary information of JeeStrap Co.
 * Copying or reproduction without prior written approval is prohibited.
 * Copyright (c) 2014 All rights reserved. =============================
 */

package com.jeestrap.common.smartcoder.util;

import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class AppUtil {

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd）
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}

	/**
	 * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		SimpleDateFormat s = new SimpleDateFormat(pattern);
		return s.format(new Date());
	}

	/**
	 * 两个数组合并，去掉重复的，然后再排序
	 * 
	 * @param arr1
	 * @param arr2
	 * @return
	 * 
	 * @since
	 */
	public static Object[] mergeArray(Object[] arr1, Object[] arr2) {
		// Set是不允许重复的，所以将数组的值全部放在Set对象中
		Set set = new LinkedHashSet<Integer>();

		for (int i = 0; i < arr1.length; i++) {
			set.add(arr1[i]);
		}

		for (int i = 0; i < arr2.length; i++) {
			set.add(arr2[i]);
		}

		// 得到的是不重复的值，Set的长度
		System.out.println(set.size());

		Iterator i = set.iterator();
		Object[] arrays = new Object[set.size()];
		int num = 0;
		while (i.hasNext()) {
			arrays[num] = i.next();
			num = num + 1;
		}
		return arrays;
	}

	/**
	 * 拆分两个数组
	 * 
	 * @param arr1
	 * @param arr2
	 * @return List<Object[]> <br/>
	 *         0: 两个数组中相同的元素 <br/>
	 *         1: 第一个数组中不同的元素 <br/>
	 *         2: 第二个数组中不同的元素
	 * 
	 * @since
	 */
	public static List<Object[]> splitArray(Object[] arr1, Object[] arr2) {
		// Set是不允许重复的，所以将数组的值全部放在Set对象中
		Set set = new LinkedHashSet<Integer>();

		for (int i = 0; i < arr1.length; i++) {
			set.add(arr1[i]);
		}

		List<Object> sameObjectList = new LinkedList<Object>();
		List<Object> diff2ObjectList = new LinkedList<Object>();
		for (int i = 0; i < arr2.length; i++) {
			if (set.contains(arr2[i])) {
				sameObjectList.add(arr2[i]);
			} else {
				diff2ObjectList.add(arr2[i]);
			}
		}

		List<Object> diff1ObjectList = new LinkedList<Object>();

		for (int j = 0; j < arr1.length; j++) {
			boolean isSame = false;
			for (int i = 0; i < sameObjectList.size(); i++) {
				if (sameObjectList.get(i).equals(arr1[j])) {
					isSame = true;
					continue;
				}
			}

			if (!isSame) {
				diff1ObjectList.add(arr1[j]);
			}
		}

		// 得到的是不重复的值，Set的长度
		System.out.println(set.size());

		List<Object[]> arrays = new ArrayList<Object[]>(3);
		arrays.add(sameObjectList.toArray());
		arrays.add(diff1ObjectList.toArray());
		arrays.add(diff2ObjectList.toArray());
		return arrays;
	}

	public static void callCmd(String locationCmd) {
		try {
			Process child = Runtime.getRuntime().exec("cmd.exe /C start " + locationCmd);
			InputStream in = child.getInputStream();
			int c;
			while ((c = in.read()) != -1) {
			}
			in.close();
			try {
				child.waitFor();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("done");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
